/*
 * Main.c
 *
 *  Created on: Jun 13, 2011
 *      Author: ben
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <inttypes.h>

#include "uart.h"
#include "s_glcd.h"

#ifndef F_CPU
#define F_CPU 20000000
#endif

#define BAUD_RATE 115200
#define UBRRVAL ((F_CPU/(BAUD_RATE*16UL))-1)
#define BTN0 0x08
#define RECEIVED_DATA_SIZE 8

/**-----------------------------------------**/
/** Function Definitions 					**/
/**-----------------------------------------**/
int main(void);
int wait_one_second(void);
int set_PORTB_bit(int position, int value);
void Print_String(char* str);
SIGNAL (SIG_INT0);

volatile int cstate;

int main()
{
	USARTInit(10);
	DDRB = 0b00110000;
	PCMSK |= (1 << PCINT3);
	PCMSK |= (1 << PCINT2);
	MCUCR = (0 << ISC01) | (1 << ISC00);
	GIMSK |= (1 << PCIE);
	sei();

	uint8_t received_data[RECEIVED_DATA_SIZE];
	uint8_t i;

	for (i = 0; i < RECEIVED_DATA_SIZE; i++)
	{
		received_data[i] = 0;
	}
	i = 0;

	onClearScreen();
	onMainMenu();
	cstate = 0;

	while (1)
	{
	}

	return 1;
}

int wait_one_second(void)
{
	_delay_ms(1000);
	return 1;
}

int set_PORTB_bit(int position, int value)
{
	if (value == 0)
		PORTB &= ~(1 << position);
	else
		PORTB |= (1 << position);

	return 1;
}

SIGNAL (SIG_PCINT)
{
	if (!(PINB & 0x08))
	{
		if (cstate != 1)
		{
			onGPSMenu();
//			onClearScreen();
//			USARTWriteString("0x08");
			cstate = 1;
			//loop_until_bit_is_set(PINB, 0x08);
//			USARTWriteString("...");
			return 0;
		}
	}

	if (!(PINB & 0x04))
	{
		if (cstate != 0)
		{
			onMainMenu();
//			onClearScreen();
//			USARTWriteString("0x04");
			cstate = 0;
			//loop_until_bit_is_set(PINB, 0x04);
//			USARTWriteString("...");
			return 0;
		}
	}
}

